package com.uottawa.busstopreminder;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.codeandmagic.android.gauge.GaugeView;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.TextView;

public class SpeedActivity extends Activity{
	private LocationListener locationListener = null;
	private LocationManager locationManager = null;
	private double speed = 0.0;
	private AbsoluteSizeSpan sizeSpanLarge = null;
	private AbsoluteSizeSpan sizeSpanSmall = null;
	private static final int gpsMinTime = 500;
	private static final int gpsMinDistance = 0;
	private static final int HOUR_MULTIPLIER = 3600;
	private GaugeView mGaugeView1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.speed);
        mGaugeView1 = (GaugeView) findViewById(R.id.gauge_view1);
        
        locationListener = new LocationListener()
		{
			@Override
			public void onLocationChanged(final Location location)
			{
				onGPSUpdate(location);
			}
			
			@Override
			public void onProviderDisabled(final String provider)
			{
			}
			
			@Override
			public void onProviderEnabled(final String provider)
			{
			}
			
			@Override
			public void onStatusChanged(final String provider, final int status, final Bundle extras)
			{
			}
		};
        
        startListening();
        
      //  ((TextView)findViewById(R.id.info_message)).setText("Waiting for GPS!");
        mGaugeView1.setTargetValue(0);
    }
    
    private void startListening()
	{
		if (locationManager == null)
		{
			locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		}
		
		final Criteria criteria = new Criteria();
		
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setSpeedRequired(true);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		
		final String bestProvider = locationManager.getBestProvider(criteria, true);
		
		if (bestProvider != null && bestProvider.length() > 0)
		{
			locationManager.requestLocationUpdates(bestProvider, gpsMinTime, gpsMinDistance, locationListener);
		}
		else
		{
			final List<String> providers = locationManager.getProviders(true);
			
			for (final String provider : providers)
			{
				locationManager.requestLocationUpdates(provider, gpsMinTime, gpsMinDistance, locationListener);
			}
		}
	}
    
	private void onGPSUpdate(Location location) 
	{
		location.getLatitude();
		location.getLongitude();
		speed = location.getSpeed();
		
		String speedString = "" + roundDecimal(convertSpeed(speed),2);
		String unitString = "km/h";
		
		//setSpeedText(R.id.info_message,speedString + " " + unitString);
		mGaugeView1.setTargetValue(Float.valueOf(speedString));
	}

	@Override
	protected void onDestroy() {
		stopListening();
		super.onDestroy();
	}
		

	private double convertSpeed(double speed){
		return ((speed * HOUR_MULTIPLIER) * 0.001); 
	}
	
	private double roundDecimal(double value, final int decimalPlace)
	{
		BigDecimal bd = new BigDecimal(value);
		
		bd = bd.setScale(decimalPlace, RoundingMode.HALF_UP);
		value = bd.doubleValue();
		
		return value;
	}
	
	private void setSpeedText(int textid,String text)
	{
		Spannable span = new SpannableString(text);
		int firstPos = text.indexOf(32);
		
		span.setSpan(sizeSpanLarge, 0, firstPos,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		span.setSpan(sizeSpanSmall, firstPos + 1, text.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		
		TextView tv = ((TextView)findViewById(textid));
		
		tv.setVisibility(View.INVISIBLE);
		//tv.setText(span);
	}
	
	private void stopListening()
	{
		try
		{
			if (locationManager != null && locationListener != null)
			{
				locationManager.removeUpdates(locationListener);
			}
			
			locationManager = null;
		}
		catch (final Exception ex)
		{
			
		}
	}

}